<?php

declare(strict_types=1);
namespace App\Controller;

use App\Model\Phone;
use Hyperf\HttpServer\Contract\RequestInterface;


class PhoneController extends AbstractController
{
    //手机号搜索
    public function index(RequestInterface  $request){
        $input = $request->all();

        $phone = Phone::select('mobile','real_price','province','city','operator')->where('status', 0);

        if( isset( $input['mobile'] ) && !empty( $input['mobile'] ) ){
            $phone->where('mobile',$input['mobile']);
        }

        if( isset( $input['part_mobile'] ) ){
            $phone->where('mobile','like','%'.$input['part_mobile'].'%');
        }

        if( isset( $input['operator'] ) && !empty( $input['operator'] ) ){
            $phone->where('operator',$input['operator']);
        }

        if( isset( $input['province'] ) && !empty( $input['province'] ) ){
            $phone->where('province',$input['province']);
        }

        if( isset( $input['city'] ) && !empty( $input['city'] ) ){
            $phone->where('city',$input['city']);
        }

        if( isset( $input['type'] ) && !empty( $input['type'] ) ){
            $phone->where('type',$input['type']);
        }

        for($i=2;$i<12;$i++){
            if( isset( $input['d'.$i] )  ){
                $phone->where('d'.$i,$input['d'.$i]);
            }
        }

        if( isset( $input['lucky_number'] ) && !empty( $input['lucky_number'] ) ){
            $phone->where('lucky_number',$input['lucky_number']);
        }

        if( isset( $input['tail_number'] )  ){
            $type_number_arr = str_split($input['tail_number'],1);
            $type_number_arr = array_reverse($type_number_arr);
            $j = 11;
            foreach ($type_number_arr as $value){
                $phone->where('d'.$j,$value);
                $j--;
            }
        }

        if( isset( $input['type_number'] ) && !empty( $input['type_number'] ) ){
            $type_number_arr = str_split($input['type_number'],1);
            $type_number_arr = array_reverse($type_number_arr);
            $j = 11;
            foreach ($type_number_arr as $value){
                $phone->where('d'.$j,$value);
                $j--;
            }
        }

        if( isset( $input['month_number'] ) && !empty( $input['month_number'] ) ){
            $phone->where('month_number',$input['month_number']);
        }

        if( isset( $input['price_low'] ) && !empty( $input['price_low'] ) ){
            $phone->where('real_price','>=',$input['price_low']);
        }

        if( isset( $input['price_high'] ) && !empty( $input['price_high'] ) ){
            $phone->where('real_price','<=',$input['price_high']);
        }

        if( isset( $input['sort_key'] ) && !empty( $input['sort_key'] ) && isset( $input['sort_type'] ) && !empty( $input['sort_type'] ) ){
            $phone->orderBy($input['sort_key'],$input['sort_type']);
        }else{
            $phone->orderBy('updated_at','asc');
        }

        if( !isset( $input['page'] ) || empty( $input['page'] ) ){
            $input['page'] = 1;
        }

        if( !isset( $input['limit'] ) || empty( $input['limit'] ) ){
            $input['limit'] = 20;
        }
        $phone->offset(($input['page'] - 1) * $input['limit'])->limit($input['limit']);

        $data = $phone->get()->toArray();

        foreach ($data as &$val){
            $val['mobile'] = substr($val['mobile'],0,3).'<span style="color:#fa6325">'.substr($val['mobile'],3,4).'</span>'.substr($val['mobile'],7,4);
        }

        return $data;
    }

    public function test(RequestInterface $request){
        $input = $request->all();
        $add_rule = config('yunlian.add_rule');
        $match = '';
        foreach ($add_rule as $key=>$value){
            if(preg_match($value,$input['mobile'])){
                $match = $key;
            }
        }
        return $match;
    }
}
